package com.live.task;

import java.net.URI;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;

import com.live.activity.R;
import com.live.common.Constant;
import com.live.tools.LiveTools;

public class LiveTeamStats extends AsyncTask<Bundle, Integer, String> {

	private HashMap<String, Object> statsDate;

	private ScrollView view;

	private String matchId;

	private String matchCategory;

	public LiveTeamStats(ScrollView view) {
		this.view = view;
	}

	// 此方法在主线程执行,当任务执行之前开始调用此方法，可以在这里显示进度对话框
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		Log.i("Comment", "onPreExecute()  " + Thread.currentThread().getId());
	}

	// 此方法在后台线程执行，完成任务的主要工作，通常需要较长的时间。
	// 在执行过程中可以调用publicProgress(Progress...)来更新任务的进度。

	@Override
	protected String doInBackground(Bundle... params) {
		Log.i("Comment", "doInBackground... " + Thread.currentThread().getId());

		matchId = params[0].getString("matchId");
		matchCategory = params[0].getString("matchCategory");

		String url = Constant.SITE_URL + "live_team_stats.php?locale=" + LiveTools.getLocaleLanguage() + "&matchId="
				+ matchId + "&matchCategory=" + matchCategory;
		statsDate = getMatchStats(url);

		publishProgress();
		return null;
	}

	// 此方法在主线程执行，用于显示任务执行的进度
	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
		Log.i("PageTask", "onProgressUpdate()  " + Thread.currentThread().getId());

		if ("1".equals(matchCategory)) {
			TextView t1_h_s_s = (TextView) view.findViewById(R.id.t1_h_s_s);
			t1_h_s_s.setText((String) statsDate.get("t1_h_s_s"));

			TextView t1_h_s_n = (TextView) view.findViewById(R.id.t1_h_s_n);
			t1_h_s_n.setText((String) statsDate.get("t1_h_s_n"));

			TextView t1_h_lb_s = (TextView) view.findViewById(R.id.t1_h_lb_s);
			t1_h_lb_s.setText((String) statsDate.get("t1_h_lb_s"));

			TextView t1_h_lb_n = (TextView) view.findViewById(R.id.t1_h_lb_n);
			t1_h_lb_n.setText((String) statsDate.get("t1_h_lb_n"));

			TextView t1_h_ass_s = (TextView) view.findViewById(R.id.t1_h_ass_s);
			t1_h_ass_s.setText((String) statsDate.get("t1_h_ass_s"));

			TextView t1_h_ass_n = (TextView) view.findViewById(R.id.t1_h_ass_n);
			t1_h_ass_n.setText((String) statsDate.get("t1_h_ass_n"));

			TextView t1_h_ste_s = (TextView) view.findViewById(R.id.t1_h_ste_s);
			t1_h_ste_s.setText((String) statsDate.get("t1_h_ste_s"));

			TextView t1_h_ste_n = (TextView) view.findViewById(R.id.t1_h_ste_n);
			t1_h_ste_n.setText((String) statsDate.get("t1_h_ste_n"));

			TextView t1_h_blo_s = (TextView) view.findViewById(R.id.t1_h_blo_s);
			t1_h_blo_s.setText((String) statsDate.get("t1_h_blo_s"));

			TextView t1_h_blo_n = (TextView) view.findViewById(R.id.t1_h_blo_n);
			t1_h_blo_n.setText((String) statsDate.get("t1_h_blo_n"));

			TextView t1_lb = (TextView) view.findViewById(R.id.t1_lb);
			t1_lb.setText((String) statsDate.get("t1_lb"));

			TextView t1_field = (TextView) view.findViewById(R.id.t1_field);
			t1_field.setText((String) statsDate.get("t1_field"));

			TextView t1_three = (TextView) view.findViewById(R.id.t1_three);
			t1_three.setText((String) statsDate.get("t1_three"));

			TextView t1_free = (TextView) view.findViewById(R.id.t1_free);
			t1_free.setText((String) statsDate.get("t1_free"));

			TextView t1_jshots = (TextView) view.findViewById(R.id.t1_jshots);
			t1_jshots.setText((String) statsDate.get("t1_jshots"));

			TextView t1_dunks = (TextView) view.findViewById(R.id.t1_dunks);
			t1_dunks.setText((String) statsDate.get("t1_dunks"));

			TextView t1_layups = (TextView) view.findViewById(R.id.t1_layups);
			t1_layups.setText((String) statsDate.get("t1_layups"));

			TextView t1_tipins = (TextView) view.findViewById(R.id.t1_tipins);
			t1_tipins.setText((String) statsDate.get("t1_tipins"));

			TextView t1_hooks = (TextView) view.findViewById(R.id.t1_hooks);
			t1_hooks.setText((String) statsDate.get("t1_hooks"));

			TextView t1_bbeaters = (TextView) view.findViewById(R.id.t1_bbeaters);
			t1_bbeaters.setText((String) statsDate.get("t1_bbeaters"));

			TextView t1_tecf = (TextView) view.findViewById(R.id.t1_tecf);
			t1_tecf.setText((String) statsDate.get("t1_tecf"));

			TextView t1_flag = (TextView) view.findViewById(R.id.t1_flag);
			t1_flag.setText((String) statsDate.get("t1_flag"));

			TextView t1_eject = (TextView) view.findViewById(R.id.t1_eject);
			t1_eject.setText((String) statsDate.get("t1_eject"));

			TextView t1_disq = (TextView) view.findViewById(R.id.t1_disq);
			t1_disq.setText((String) statsDate.get("t1_disq"));

			// 客队
			TextView t2_h_s_s = (TextView) view.findViewById(R.id.t2_h_s_s);
			t2_h_s_s.setText((String) statsDate.get("t2_h_s_s"));

			TextView t2_h_s_n = (TextView) view.findViewById(R.id.t2_h_s_n);
			t2_h_s_n.setText((String) statsDate.get("t2_h_s_n"));

			TextView t2_h_lb_s = (TextView) view.findViewById(R.id.t2_h_lb_s);
			t2_h_lb_s.setText((String) statsDate.get("t2_h_lb_s"));

			TextView t2_h_lb_n = (TextView) view.findViewById(R.id.t2_h_lb_n);
			t2_h_lb_n.setText((String) statsDate.get("t2_h_lb_n"));

			TextView t2_h_ass_s = (TextView) view.findViewById(R.id.t2_h_ass_s);
			t2_h_ass_s.setText((String) statsDate.get("t2_h_ass_s"));

			TextView t2_h_ass_n = (TextView) view.findViewById(R.id.t2_h_ass_n);
			t2_h_ass_n.setText((String) statsDate.get("t2_h_ass_n"));

			TextView t2_h_ste_s = (TextView) view.findViewById(R.id.t2_h_ste_s);
			t2_h_ste_s.setText((String) statsDate.get("t2_h_ste_s"));

			TextView t2_h_ste_n = (TextView) view.findViewById(R.id.t2_h_ste_n);
			t2_h_ste_n.setText((String) statsDate.get("t2_h_ste_n"));

			TextView t2_h_blo_s = (TextView) view.findViewById(R.id.t2_h_blo_s);
			t2_h_blo_s.setText((String) statsDate.get("t2_h_blo_s"));

			TextView t2_h_blo_n = (TextView) view.findViewById(R.id.t2_h_blo_n);
			t2_h_blo_n.setText((String) statsDate.get("t2_h_blo_n"));

			TextView t2_lb = (TextView) view.findViewById(R.id.t2_lb);
			t2_lb.setText((String) statsDate.get("t2_lb"));

			TextView t2_field = (TextView) view.findViewById(R.id.t2_field);
			t2_field.setText((String) statsDate.get("t2_field"));

			TextView t2_three = (TextView) view.findViewById(R.id.t2_three);
			t2_three.setText((String) statsDate.get("t2_three"));

			TextView t2_free = (TextView) view.findViewById(R.id.t2_free);
			t2_free.setText((String) statsDate.get("t2_free"));

			TextView t2_jshots = (TextView) view.findViewById(R.id.t2_jshots);
			t2_jshots.setText((String) statsDate.get("t2_jshots"));

			TextView t2_dunks = (TextView) view.findViewById(R.id.t2_dunks);
			t2_dunks.setText((String) statsDate.get("t2_dunks"));

			TextView t2_layups = (TextView) view.findViewById(R.id.t2_layups);
			t2_layups.setText((String) statsDate.get("t2_layups"));

			TextView t2_tipins = (TextView) view.findViewById(R.id.t2_tipins);
			t2_tipins.setText((String) statsDate.get("t2_tipins"));

			TextView t2_hooks = (TextView) view.findViewById(R.id.t2_hooks);
			t2_hooks.setText((String) statsDate.get("t2_hooks"));

			TextView t2_bbeaters = (TextView) view.findViewById(R.id.t2_bbeaters);
			t2_bbeaters.setText((String) statsDate.get("t2_bbeaters"));

			TextView t2_tecf = (TextView) view.findViewById(R.id.t2_tecf);
			t2_tecf.setText((String) statsDate.get("t2_tecf"));

			TextView t2_flag = (TextView) view.findViewById(R.id.t2_flag);
			t2_flag.setText((String) statsDate.get("t2_flag"));

			TextView t2_eject = (TextView) view.findViewById(R.id.t2_eject);
			t2_eject.setText((String) statsDate.get("t2_eject"));

			TextView t2_disq = (TextView) view.findViewById(R.id.t2_disq);
			t2_disq.setText((String) statsDate.get("t2_disq"));
		} else if ("2".equals(matchCategory)) {
			// 主队

			TextView t1_z_s_m = (TextView) view.findViewById(R.id.t1_z_s_m);
			t1_z_s_m.setText((String) statsDate.get("t1_z_s_m"));

			TextView t1_s_z = (TextView) view.findViewById(R.id.t1_s_z);
			t1_s_z.setText((String) statsDate.get("t1_s_z"));

			TextView t1_s_p = (TextView) view.findViewById(R.id.t1_s_p);
			t1_s_p.setText((String) statsDate.get("t1_s_p"));

			TextView t1_y_w = (TextView) view.findViewById(R.id.t1_y_w);
			t1_y_w.setText((String) statsDate.get("t1_y_w"));

			TextView t1_q_d = (TextView) view.findViewById(R.id.t1_q_d);
			t1_q_d.setText((String) statsDate.get("t1_q_d"));

			TextView t1_r_y = (TextView) view.findViewById(R.id.t1_r_y);
			t1_r_y.setText((String) statsDate.get("t1_r_y"));

			TextView t1_j_q = (TextView) view.findViewById(R.id.t1_j_q);
			t1_j_q.setText((String) statsDate.get("t1_j_q"));

			TextView t1_j_w = (TextView) view.findViewById(R.id.t1_j_w);
			t1_j_w.setText((String) statsDate.get("t1_j_w"));

			TextView t1_c_q_l = (TextView) view.findViewById(R.id.t1_c_q_l);
			t1_c_q_l.setText((String) statsDate.get("t1_c_q_l"));

			TextView t1_c_z_l = (TextView) view.findViewById(R.id.t1_c_z_l);
			t1_c_z_l.setText((String) statsDate.get("t1_c_z_l"));

			TextView t1_q_d_l = (TextView) view.findViewById(R.id.t1_q_d_l);
			t1_q_d_l.setText((String) statsDate.get("t1_q_d_l"));

			TextView t1_t_q_l = (TextView) view.findViewById(R.id.t1_t_q_l);
			t1_t_q_l.setText((String) statsDate.get("t1_t_q_l"));

			TextView t1_k_q_l = (TextView) view.findViewById(R.id.t1_k_q_l);
			t1_k_q_l.setText((String) statsDate.get("t1_k_q_l"));

			// 客队
			TextView t2_z_s_m = (TextView) view.findViewById(R.id.t2_z_s_m);
			t2_z_s_m.setText((String) statsDate.get("t2_z_s_m"));

			TextView t2_s_z = (TextView) view.findViewById(R.id.t2_s_z);
			t2_s_z.setText((String) statsDate.get("t2_s_z"));

			TextView t2_s_p = (TextView) view.findViewById(R.id.t2_s_p);
			t2_s_p.setText((String) statsDate.get("t2_s_p"));

			TextView t2_y_w = (TextView) view.findViewById(R.id.t2_y_w);
			t2_y_w.setText((String) statsDate.get("t2_y_w"));

			TextView t2_q_d = (TextView) view.findViewById(R.id.t2_q_d);
			t2_q_d.setText((String) statsDate.get("t2_q_d"));

			TextView t2_r_y = (TextView) view.findViewById(R.id.t2_r_y);
			t2_r_y.setText((String) statsDate.get("t2_r_y"));

			TextView t2_j_q = (TextView) view.findViewById(R.id.t2_j_q);
			t2_j_q.setText((String) statsDate.get("t2_j_q"));

			TextView t2_j_w = (TextView) view.findViewById(R.id.t2_j_w);
			t2_j_w.setText((String) statsDate.get("t2_j_w"));

			TextView t2_c_q_l = (TextView) view.findViewById(R.id.t2_c_q_l);
			t2_c_q_l.setText((String) statsDate.get("t2_c_q_l"));

			TextView t2_c_z_l = (TextView) view.findViewById(R.id.t2_c_z_l);
			t2_c_z_l.setText((String) statsDate.get("t2_c_z_l"));

			TextView t2_q_d_l = (TextView) view.findViewById(R.id.t2_q_d_l);
			t2_q_d_l.setText((String) statsDate.get("t2_q_d_l"));

			TextView t2_t_q_l = (TextView) view.findViewById(R.id.t2_t_q_l);
			t2_t_q_l.setText((String) statsDate.get("t2_t_q_l"));

			TextView t2_k_q_l = (TextView) view.findViewById(R.id.t2_k_q_l);
			t2_k_q_l.setText((String) statsDate.get("t2_k_q_l"));
		}
	}

	// 此方法在主线程执行，任务执行的结果作为此方法的参数返回。
	@Override
	protected void onPostExecute(String result) {
		// tv.setText(result);
		Log.i("PageTask", "onPostExecute()  " + Thread.currentThread().getId());
	}

	private HashMap<String, Object> getMatchStats(String url) {
		HashMap<String, Object> data = new HashMap<String, Object>();
		HttpClient client = new DefaultHttpClient();
		// 提拱默认的HttpClient实现
		HttpPost request;
		try {
			request = new HttpPost(new URI(url));
			HttpResponse response = client.execute(request);
			// 判断请求是否成功
			if (response.getStatusLine().getStatusCode() == 200) { // 200表示请求成功
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String out = EntityUtils.toString(entity);
					try {
						String result = new JSONObject(out).getString("result");
						if (Constant.Y.equals(result)) {
							JSONArray matches = new JSONObject(out).getJSONArray("results");
							for (int i = 0; i < matches.length(); i++) {
								if ("1".equals(matchCategory)) {
									// 主队
									data.put("t1_h_s_s", (String) matches.getJSONObject(i).get("t1_h_s_s"));

									data.put("t1_h_s_n", (String) matches.getJSONObject(i).get("t1_h_s_n"));

									data.put("t1_h_lb_s", (String) matches.getJSONObject(i).get("t1_h_lb_s"));

									data.put("t1_h_lb_n", (String) matches.getJSONObject(i).get("t1_h_lb_n"));

									data.put("t1_h_ass_s", (String) matches.getJSONObject(i).get("t1_h_ass_s"));

									data.put("t1_h_ass_n", (String) matches.getJSONObject(i).get("t1_h_ass_n"));

									data.put("t1_h_ste_s", (String) matches.getJSONObject(i).get("t1_h_ste_s"));

									data.put("t1_h_ste_n", (String) matches.getJSONObject(i).get("t1_h_ste_n"));

									data.put("t1_h_blo_s", (String) matches.getJSONObject(i).get("t1_h_blo_s"));

									data.put("t1_h_blo_n", (String) matches.getJSONObject(i).get("t1_h_blo_n"));

									//
									data.put("t1_lb", (String) matches.getJSONObject(i).get("t1_lb"));

									data.put("t1_field", (String) matches.getJSONObject(i).get("t1_field"));

									data.put("t1_three", (String) matches.getJSONObject(i).get("t1_three"));

									data.put("t1_free", (String) matches.getJSONObject(i).get("t1_free"));

									data.put("t1_jshots", (String) matches.getJSONObject(i).get("t1_jshots"));

									data.put("t1_dunks", (String) matches.getJSONObject(i).get("t1_dunks"));

									data.put("t1_layups", (String) matches.getJSONObject(i).get("t1_layups"));

									data.put("t1_tipins", (String) matches.getJSONObject(i).get("t1_tipins"));

									data.put("t1_hooks", (String) matches.getJSONObject(i).get("t1_hooks"));

									data.put("t1_bbeaters", (String) matches.getJSONObject(i).get("t1_bbeaters"));

									data.put("t1_tecf", (String) matches.getJSONObject(i).get("t1_tecf"));

									data.put("t1_flag", (String) matches.getJSONObject(i).get("t1_flag"));

									data.put("t1_eject", (String) matches.getJSONObject(i).get("t1_eject"));

									data.put("t1_disq", (String) matches.getJSONObject(i).get("t1_disq"));

									// 客队
									data.put("t2_h_s_s", (String) matches.getJSONObject(i).get("t2_h_s_s"));

									data.put("t2_h_s_n", (String) matches.getJSONObject(i).get("t2_h_s_n"));

									data.put("t2_h_lb_s", (String) matches.getJSONObject(i).get("t2_h_lb_s"));

									data.put("t2_h_lb_n", (String) matches.getJSONObject(i).get("t2_h_lb_n"));

									data.put("t2_h_ass_s", (String) matches.getJSONObject(i).get("t2_h_ass_s"));

									data.put("t2_h_ass_n", (String) matches.getJSONObject(i).get("t2_h_ass_n"));

									data.put("t2_h_ste_s", (String) matches.getJSONObject(i).get("t2_h_ste_s"));

									data.put("t2_h_ste_n", (String) matches.getJSONObject(i).get("t2_h_ste_n"));

									data.put("t2_h_blo_s", (String) matches.getJSONObject(i).get("t2_h_blo_s"));

									data.put("t2_h_blo_n", (String) matches.getJSONObject(i).get("t2_h_blo_n"));

									//

									data.put("t2_lb", (String) matches.getJSONObject(i).get("t2_lb"));

									data.put("t2_field", (String) matches.getJSONObject(i).get("t2_field"));

									data.put("t2_three", (String) matches.getJSONObject(i).get("t2_three"));

									data.put("t2_free", (String) matches.getJSONObject(i).get("t2_free"));

									data.put("t2_jshots", (String) matches.getJSONObject(i).get("t2_jshots"));

									data.put("t2_dunks", (String) matches.getJSONObject(i).get("t2_dunks"));

									data.put("t2_layups", (String) matches.getJSONObject(i).get("t2_layups"));

									data.put("t2_tipins", (String) matches.getJSONObject(i).get("t2_tipins"));

									data.put("t2_hooks", (String) matches.getJSONObject(i).get("t2_hooks"));

									data.put("t2_bbeaters", (String) matches.getJSONObject(i).get("t2_bbeaters"));

									data.put("t2_tecf", (String) matches.getJSONObject(i).get("t2_tecf"));

									data.put("t2_flag", (String) matches.getJSONObject(i).get("t2_flag"));

									data.put("t2_eject", (String) matches.getJSONObject(i).get("t2_eject"));

									data.put("t2_disq", (String) matches.getJSONObject(i).get("t2_disq"));
								} else if ("2".equals(matchCategory)) {
									//
									data.put("t1_z_s_m", (String) matches.getJSONObject(i).get("t1_z_s_m"));

									data.put("t1_s_z", (String) matches.getJSONObject(i).get("t1_s_z"));

									data.put("t1_s_p", (String) matches.getJSONObject(i).get("t1_s_p"));

									data.put("t1_y_w", (String) matches.getJSONObject(i).get("t1_y_w"));

									data.put("t1_q_d", (String) matches.getJSONObject(i).get("t1_q_d"));

									data.put("t1_r_y", (String) matches.getJSONObject(i).get("t1_r_y"));

									data.put("t1_j_q", (String) matches.getJSONObject(i).get("t1_j_q"));

									data.put("t1_j_w", (String) matches.getJSONObject(i).get("t1_j_w"));

									data.put("t1_c_q_l", (String) matches.getJSONObject(i).get("t1_c_q_l"));

									data.put("t1_c_z_l", (String) matches.getJSONObject(i).get("t1_c_z_l"));

									data.put("t1_q_d_l", (String) matches.getJSONObject(i).get("t1_q_d_l"));

									data.put("t1_t_q_l", (String) matches.getJSONObject(i).get("t1_t_q_l"));

									data.put("t1_k_q_l", (String) matches.getJSONObject(i).get("t1_k_q_l"));

									//
									data.put("t2_z_s_m", (String) matches.getJSONObject(i).get("t2_z_s_m"));

									data.put("t2_s_z", (String) matches.getJSONObject(i).get("t2_s_z"));

									data.put("t2_s_p", (String) matches.getJSONObject(i).get("t2_s_p"));

									data.put("t2_y_w", (String) matches.getJSONObject(i).get("t2_y_w"));

									data.put("t2_q_d", (String) matches.getJSONObject(i).get("t2_q_d"));

									data.put("t2_r_y", (String) matches.getJSONObject(i).get("t2_r_y"));

									data.put("t2_j_q", (String) matches.getJSONObject(i).get("t2_j_q"));

									data.put("t2_j_w", (String) matches.getJSONObject(i).get("t2_j_w"));

									data.put("t2_c_q_l", (String) matches.getJSONObject(i).get("t2_c_q_l"));

									data.put("t2_c_z_l", (String) matches.getJSONObject(i).get("t2_c_z_l"));

									data.put("t2_q_d_l", (String) matches.getJSONObject(i).get("t2_q_d_l"));

									data.put("t2_t_q_l", (String) matches.getJSONObject(i).get("t2_t_q_l"));

									data.put("t2_k_q_l", (String) matches.getJSONObject(i).get("t2_k_q_l"));
								}
							}
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}

}